package com.mvp.shq.washer.ui.faultFeedBack;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.LabelAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.Label;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.T;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FaultFeedBackActivity extends BaseActivity<Presenter, Model> implements FaultFeedBackConstract.View {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.et_washer_no)
    EditText etWasherNo;
    @BindView(R.id.et_fault_des)
    EditText etFaultDes;
    @BindView(R.id.tv_fault_des)
    TextView tvFaultDes;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    @BindView(R.id.tv_fault_prompt)
    TextView tvPrompt;
    private List<Label> labels;
    private LabelAdapter adapter;
    private String[] labelStr = new String[]{"脱水不转","不注水","边进水边排水","转速慢","底部漏水","进水不止","连接故障","其他"};
    @Override
    public int getLayoutId() {
        return R.layout.activity_fault_feed_back;
    }

    @Override
    public void initView() {
        etFaultDes.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvFaultDes.setText(etFaultDes.getText().toString().length()+"/200");
//                if (etFaultDes.getText().toString().length() < 10){
//                    tvPrompt.setVisibility(View.VISIBLE);
//                }else {
//                    tvPrompt.setVisibility(View.GONE);
//                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        recyclerview.setLayoutManager(new GridLayoutManager(context,3, LinearLayoutManager.VERTICAL,false));
        RecyclerViewDivider divider1 = new RecyclerViewDivider(context,LinearLayoutManager.VERTICAL,20, ContextCompat.getColor(context,R.color.white));
        recyclerview.addItemDecoration(divider1);
        labels = new ArrayList<>();
        for (int i=0;i<labelStr.length;i++){
            Label label = new Label();
            label.setLabelName(labelStr[i]);
            labels.add(label);
        }
        adapter = new LabelAdapter(context,labels);
        recyclerview.setAdapter(adapter);
    }
    @Subscribe(code = EventCode.ON_LABEL_CLICK)
    public void onLabelClick(Integer position){
        labels.get(position).setSelected(!labels.get(position).isSelected());
        adapter.notifyDataSetChanged();
    }

    @OnClick({R.id.back, R.id.btn_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn_commit:
                String washerNo = etWasherNo.getText().toString();
                String des = etFaultDes.getText().toString();
                String content = "";
                if (TextUtils.isEmpty(washerNo) || washerNo.equals(" ")){
                    T.show("请填写洗衣机编号");
                    return;
                }
                if (TextUtils.isEmpty(des)){
                    T.show("请填写故障描述");
                    return;
                }
                if (des.length() < 10){
                    T.show("故障描述内容不能少于10个字符");
                    return;
                }
                for (int i=0;i<labels.size();i++){
                    if (labels.get(i).isSelected()){
                        content += labels.get(i).getLabelName();
                        content += ";";
                    }
                }
                content += des;
                showLoadingDialog(true);
                presenter.faultFeedBack(washerNo,content);
                break;
        }
    }

    @Override
    public void feedBackS() {
        T.show("提交成功");
        finish();
    }

    @Override
    public void feedBackF(String msg) {
        T.show(msg);
    }
}
